package com.warom.sdg.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.warom.sdg.database.SdgDatabase;
import com.warom.sdg.dto.PageRequestDto;
import com.warom.sdg.dto.PageResponseDto;
import com.warom.sdg.model.AlarmRecordEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 报警记录DAO实现类
 */
public class AlarmRecordDao_Impl implements AlarmRecordDao {
    private static final String TAG = "AlarmRecordDao";
    private final SdgDatabase database;
    
    public AlarmRecordDao_Impl(SdgDatabase database) {
        this.database = database;
    }
    
    @Override
    public long insertAlarmRecord(AlarmRecordEntity alarmRecord) {
        SQLiteDatabase db = database.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(SdgDatabase.COLUMN_ALARM_TYPE, alarmRecord.getAlarmType());
        values.put(SdgDatabase.COLUMN_PARAM_TYPE, alarmRecord.getParamType());
        values.put(SdgDatabase.COLUMN_DEVICE_ID, alarmRecord.getDeviceId());
        values.put(SdgDatabase.COLUMN_LOCATION, alarmRecord.getLocation());
        values.put(SdgDatabase.COLUMN_VALUE, alarmRecord.getValue());
        values.put(SdgDatabase.COLUMN_UNIT, alarmRecord.getUnit());
        values.put(SdgDatabase.COLUMN_THRESHOLD_MIN, alarmRecord.getThresholdMin());
        values.put(SdgDatabase.COLUMN_THRESHOLD_MAX, alarmRecord.getThresholdMax());
        values.put(SdgDatabase.COLUMN_LEVEL, alarmRecord.getLevel());
        values.put(SdgDatabase.COLUMN_STATUS, alarmRecord.getStatus());
        values.put(SdgDatabase.COLUMN_DESCRIPTION, alarmRecord.getDescription());
        values.put(SdgDatabase.COLUMN_CREATE_TIME, alarmRecord.getCreateTime());
        values.put(SdgDatabase.COLUMN_CREATE_TIME_STRING, alarmRecord.getCreateTimeString());
        
        long id = -1;
        try {
            id = db.insert(SdgDatabase.TABLE_ALARM_RECORDS, null, values);
        } catch (Exception e) {
            Log.e(TAG, "插入报警记录失败: " + e.getMessage());
        }
        
        return id;
    }
    
    @Override
    public int updateAlarmRecord(AlarmRecordEntity alarmRecord) {
        SQLiteDatabase db = database.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(SdgDatabase.COLUMN_ALARM_TYPE, alarmRecord.getAlarmType());
        values.put(SdgDatabase.COLUMN_PARAM_TYPE, alarmRecord.getParamType());
        values.put(SdgDatabase.COLUMN_DEVICE_ID, alarmRecord.getDeviceId());
        values.put(SdgDatabase.COLUMN_LOCATION, alarmRecord.getLocation());
        values.put(SdgDatabase.COLUMN_VALUE, alarmRecord.getValue());
        values.put(SdgDatabase.COLUMN_UNIT, alarmRecord.getUnit());
        values.put(SdgDatabase.COLUMN_THRESHOLD_MIN, alarmRecord.getThresholdMin());
        values.put(SdgDatabase.COLUMN_THRESHOLD_MAX, alarmRecord.getThresholdMax());
        values.put(SdgDatabase.COLUMN_LEVEL, alarmRecord.getLevel());
        values.put(SdgDatabase.COLUMN_STATUS, alarmRecord.getStatus());
        values.put(SdgDatabase.COLUMN_DESCRIPTION, alarmRecord.getDescription());
        values.put(SdgDatabase.COLUMN_CREATE_TIME, alarmRecord.getCreateTime());
        values.put(SdgDatabase.COLUMN_CREATE_TIME_STRING, alarmRecord.getCreateTimeString());
        values.put(SdgDatabase.COLUMN_HANDLE_TIME, alarmRecord.getHandleTime());
        values.put(SdgDatabase.COLUMN_HANDLE_USER, alarmRecord.getHandleUser());
        values.put(SdgDatabase.COLUMN_HANDLE_DESCRIPTION, alarmRecord.getHandleDescription());
        
        int rowsAffected = 0;
        try {
            rowsAffected = db.update(
                SdgDatabase.TABLE_ALARM_RECORDS,
                values,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(alarmRecord.getId())}
            );
        } catch (Exception e) {
            Log.e(TAG, "更新报警记录失败: " + e.getMessage());
        }
        
        return rowsAffected;
    }
    
    @Override
    public int updateAlarmStatus(long id, String status, String handleUser, String handleDescription) {
        SQLiteDatabase db = database.getWritableDatabase();
        ContentValues values = new ContentValues();
        
        values.put(SdgDatabase.COLUMN_STATUS, status);
        values.put(SdgDatabase.COLUMN_HANDLE_TIME, System.currentTimeMillis());
        values.put(SdgDatabase.COLUMN_HANDLE_USER, handleUser);
        values.put(SdgDatabase.COLUMN_HANDLE_DESCRIPTION, handleDescription);
        
        int rowsAffected = 0;
        try {
            rowsAffected = db.update(
                SdgDatabase.TABLE_ALARM_RECORDS,
                values,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)}
            );
        } catch (Exception e) {
            Log.e(TAG, "更新报警状态失败: " + e.getMessage());
        }
        
        return rowsAffected;
    }
    
    @Override
    public AlarmRecordEntity getAlarmRecordById(long id) {
        SQLiteDatabase db = database.getReadableDatabase();
        AlarmRecordEntity alarmRecord = null;
        
        try {
            Cursor cursor = db.query(
                SdgDatabase.TABLE_ALARM_RECORDS,
                null,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)},
                null,
                null,
                null
            );
            
            if (cursor != null && cursor.moveToFirst()) {
                alarmRecord = cursorToAlarmRecord(cursor);
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "查询报警记录失败: " + e.getMessage());
        }
        
        return alarmRecord;
    }
    
    @Override
    public List<AlarmRecordEntity> getAllAlarmRecords() {
        SQLiteDatabase db = database.getReadableDatabase();
        List<AlarmRecordEntity> alarmRecords = new ArrayList<>();
        
        try {
            Cursor cursor = db.query(
                SdgDatabase.TABLE_ALARM_RECORDS,
                null,
                null,
                null,
                null,
                null,
                SdgDatabase.COLUMN_CREATE_TIME + " DESC"
            );
            
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    alarmRecords.add(cursorToAlarmRecord(cursor));
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "查询所有报警记录失败: " + e.getMessage());
        }
        
        return alarmRecords;
    }
    
    @Override
    public PageResponseDto<AlarmRecordEntity> getAlarmRecordsByPage(PageRequestDto pageRequest) {
        SQLiteDatabase db = database.getReadableDatabase();
        List<AlarmRecordEntity> alarmRecords = new ArrayList<>();
        int totalCount = 0;
        
        try {
            // 查询总数
            Cursor countCursor = db.rawQuery("SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS, null);
            if (countCursor != null) {
                if (countCursor.moveToFirst()) {
                    totalCount = countCursor.getInt(0);
                }
                countCursor.close();
            }
            
            // 分页查询数据
            int offset = (pageRequest.getPage() - 1) * pageRequest.getPageSize();
            String limit = offset + "," + pageRequest.getPageSize();
            
            Cursor cursor = db.query(
                SdgDatabase.TABLE_ALARM_RECORDS,
                null,
                null,
                null,
                null,
                null,
                SdgDatabase.COLUMN_CREATE_TIME + " DESC",
                limit
            );
            
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    alarmRecords.add(cursorToAlarmRecord(cursor));
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "分页查询报警记录失败: " + e.getMessage());
        }
        
        return new PageResponseDto<AlarmRecordEntity>(pageRequest.getPage(), pageRequest.getPageSize(), totalCount, alarmRecords);
    }
    
    @Override
    public PageResponseDto<AlarmRecordEntity> getAlarmRecordsByType(String alarmType, PageRequestDto pageRequest) {
        SQLiteDatabase db = database.getReadableDatabase();
        List<AlarmRecordEntity> alarmRecords = new ArrayList<>();
        int totalCount = 0;
        
        try {
            // 查询总数
            Cursor countCursor = db.rawQuery(
                "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS + 
                " WHERE " + SdgDatabase.COLUMN_ALARM_TYPE + " = ?",
                new String[]{alarmType}
            );
            
            if (countCursor != null) {
                if (countCursor.moveToFirst()) {
                    totalCount = countCursor.getInt(0);
                }
                countCursor.close();
            }
            
            // 分页查询数据
            int offset = (pageRequest.getPage() - 1) * pageRequest.getPageSize();
            String limit = offset + "," + pageRequest.getPageSize();
            
            Cursor cursor = db.query(
                SdgDatabase.TABLE_ALARM_RECORDS,
                null,
                SdgDatabase.COLUMN_ALARM_TYPE + " = ?",
                new String[]{alarmType},
                null,
                null,
                SdgDatabase.COLUMN_CREATE_TIME + " DESC",
                limit
            );
            
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    alarmRecords.add(cursorToAlarmRecord(cursor));
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "根据类型分页查询报警记录失败: " + e.getMessage());
        }
        
        return new PageResponseDto<AlarmRecordEntity>(pageRequest.getPage(), pageRequest.getPageSize(), totalCount, alarmRecords);
    }
    
    @Override
    public PageResponseDto<AlarmRecordEntity> queryAlarmRecords(String alarmType, String paramType, 
                                                              String level, String status, 
                                                              long startTime, long endTime, 
                                                              PageRequestDto pageRequest) {
        SQLiteDatabase db = database.getReadableDatabase();
        List<AlarmRecordEntity> alarmRecords = new ArrayList<>();
        int totalCount = 0;
        
        try {
            StringBuilder whereClause = new StringBuilder();
            List<String> whereArgs = new ArrayList<>();
            
            // 构建查询条件
            if (alarmType != null && !alarmType.equals("all")) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_ALARM_TYPE + " = ?");
                whereArgs.add(alarmType);
            }
            
            if (paramType != null && !paramType.equals("all")) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_PARAM_TYPE + " = ?");
                whereArgs.add(paramType);
            }
            
            if (level != null && !level.equals("all")) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_LEVEL + " = ?");
                whereArgs.add(level);
            }
            
            if (status != null && !status.equals("all")) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_STATUS + " = ?");
                whereArgs.add(status);
            }
            
            if (startTime > 0) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_CREATE_TIME + " >= ?");
                whereArgs.add(String.valueOf(startTime));
            }
            
            if (endTime > 0) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_CREATE_TIME + " <= ?");
                whereArgs.add(String.valueOf(endTime));
            }
            
            String[] whereArgsArray = whereArgs.toArray(new String[0]);
            
            // 查询总数
            String countQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                countQuery += " WHERE " + whereClause;
            }
            
            Cursor countCursor = db.rawQuery(countQuery, whereArgsArray);
            if (countCursor != null) {
                if (countCursor.moveToFirst()) {
                    totalCount = countCursor.getInt(0);
                }
                countCursor.close();
            }
            
            // 分页查询数据
            int offset = (pageRequest.getPage() - 1) * pageRequest.getPageSize();
            String limit = offset + "," + pageRequest.getPageSize();
            
            Cursor cursor = db.query(
                SdgDatabase.TABLE_ALARM_RECORDS,
                null,
                whereClause.length() > 0 ? whereClause.toString() : null,
                whereArgsArray.length > 0 ? whereArgsArray : null,
                null,
                null,
                SdgDatabase.COLUMN_CREATE_TIME + " DESC",
                limit
            );
            
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    alarmRecords.add(cursorToAlarmRecord(cursor));
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "多条件查询报警记录失败: " + e.getMessage());
        }
        
        return new PageResponseDto<AlarmRecordEntity>(pageRequest.getPage(), pageRequest.getPageSize(), totalCount, alarmRecords);
    }
    
    @Override
    public int deleteAlarmRecord(long id) {
        SQLiteDatabase db = database.getWritableDatabase();
        int rowsAffected = 0;
        
        try {
            rowsAffected = db.delete(
                SdgDatabase.TABLE_ALARM_RECORDS,
                SdgDatabase.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)}
            );
        } catch (Exception e) {
            Log.e(TAG, "删除报警记录失败: " + e.getMessage());
        }
        
        return rowsAffected;
    }
    
    @Override
    public int deleteAlarmRecordsBefore(long timestamp) {
        SQLiteDatabase db = database.getWritableDatabase();
        int rowsAffected = 0;
        
        try {
            rowsAffected = db.delete(
                SdgDatabase.TABLE_ALARM_RECORDS,
                SdgDatabase.COLUMN_CREATE_TIME + " < ?",
                new String[]{String.valueOf(timestamp)}
            );
        } catch (Exception e) {
            Log.e(TAG, "删除历史报警记录失败: " + e.getMessage());
        }
        
        return rowsAffected;
    }
    
    @Override
    public int[] countAlarmsByLevel(String alarmType, long startTime, long endTime) {
        SQLiteDatabase db = database.getReadableDatabase();
        int[] counts = new int[3]; // HIGH, MEDIUM, LOW
        
        try {
            StringBuilder whereClause = new StringBuilder();
            List<String> whereArgs = new ArrayList<>();
            
            // 构建查询条件
            if (alarmType != null && !alarmType.equals("all")) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_ALARM_TYPE + " = ?");
                whereArgs.add(alarmType);
            }
            
            if (startTime > 0) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_CREATE_TIME + " >= ?");
                whereArgs.add(String.valueOf(startTime));
            }
            
            if (endTime > 0) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_CREATE_TIME + " <= ?");
                whereArgs.add(String.valueOf(endTime));
            }
            
            // 查询高级别报警数量
            String highQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                highQuery += " WHERE " + whereClause + " AND " + SdgDatabase.COLUMN_LEVEL + " = 'HIGH'";
            } else {
                highQuery += " WHERE " + SdgDatabase.COLUMN_LEVEL + " = 'HIGH'";
            }
            
            Cursor highCursor = db.rawQuery(highQuery, whereArgs.toArray(new String[0]));
            if (highCursor != null) {
                if (highCursor.moveToFirst()) {
                    counts[0] = highCursor.getInt(0);
                }
                highCursor.close();
            }
            
            // 查询中级别报警数量
            String mediumQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                mediumQuery += " WHERE " + whereClause + " AND " + SdgDatabase.COLUMN_LEVEL + " = 'MEDIUM'";
            } else {
                mediumQuery += " WHERE " + SdgDatabase.COLUMN_LEVEL + " = 'MEDIUM'";
            }
            
            Cursor mediumCursor = db.rawQuery(mediumQuery, whereArgs.toArray(new String[0]));
            if (mediumCursor != null) {
                if (mediumCursor.moveToFirst()) {
                    counts[1] = mediumCursor.getInt(0);
                }
                mediumCursor.close();
            }
            
            // 查询低级别报警数量
            String lowQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                lowQuery += " WHERE " + whereClause + " AND " + SdgDatabase.COLUMN_LEVEL + " = 'LOW'";
            } else {
                lowQuery += " WHERE " + SdgDatabase.COLUMN_LEVEL + " = 'LOW'";
            }
            
            Cursor lowCursor = db.rawQuery(lowQuery, whereArgs.toArray(new String[0]));
            if (lowCursor != null) {
                if (lowCursor.moveToFirst()) {
                    counts[2] = lowCursor.getInt(0);
                }
                lowCursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "统计报警级别数量失败: " + e.getMessage());
        }
        
        return counts;
    }
    
    @Override
    public int[] countAlarmsByStatus(String alarmType, long startTime, long endTime) {
        SQLiteDatabase db = database.getReadableDatabase();
        int[] counts = new int[3]; // ACTIVE, HANDLED, IGNORED
        
        try {
            StringBuilder whereClause = new StringBuilder();
            List<String> whereArgs = new ArrayList<>();
            
            // 构建查询条件
            if (alarmType != null && !alarmType.equals("all")) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_ALARM_TYPE + " = ?");
                whereArgs.add(alarmType);
            }
            
            if (startTime > 0) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_CREATE_TIME + " >= ?");
                whereArgs.add(String.valueOf(startTime));
            }
            
            if (endTime > 0) {
                appendWhereClause(whereClause, SdgDatabase.COLUMN_CREATE_TIME + " <= ?");
                whereArgs.add(String.valueOf(endTime));
            }
            
            // 查询未处理报警数量
            String activeQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                activeQuery += " WHERE " + whereClause + " AND " + SdgDatabase.COLUMN_STATUS + " = 'ACTIVE'";
            } else {
                activeQuery += " WHERE " + SdgDatabase.COLUMN_STATUS + " = 'ACTIVE'";
            }
            
            Cursor activeCursor = db.rawQuery(activeQuery, whereArgs.toArray(new String[0]));
            if (activeCursor != null) {
                if (activeCursor.moveToFirst()) {
                    counts[0] = activeCursor.getInt(0);
                }
                activeCursor.close();
            }
            
            // 查询已处理报警数量
            String handledQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                handledQuery += " WHERE " + whereClause + " AND " + SdgDatabase.COLUMN_STATUS + " = 'HANDLED'";
            } else {
                handledQuery += " WHERE " + SdgDatabase.COLUMN_STATUS + " = 'HANDLED'";
            }
            
            Cursor handledCursor = db.rawQuery(handledQuery, whereArgs.toArray(new String[0]));
            if (handledCursor != null) {
                if (handledCursor.moveToFirst()) {
                    counts[1] = handledCursor.getInt(0);
                }
                handledCursor.close();
            }
            
            // 查询已忽略报警数量
            String ignoredQuery = "SELECT COUNT(*) FROM " + SdgDatabase.TABLE_ALARM_RECORDS;
            if (whereClause.length() > 0) {
                ignoredQuery += " WHERE " + whereClause + " AND " + SdgDatabase.COLUMN_STATUS + " = 'IGNORED'";
            } else {
                ignoredQuery += " WHERE " + SdgDatabase.COLUMN_STATUS + " = 'IGNORED'";
            }
            
            Cursor ignoredCursor = db.rawQuery(ignoredQuery, whereArgs.toArray(new String[0]));
            if (ignoredCursor != null) {
                if (ignoredCursor.moveToFirst()) {
                    counts[2] = ignoredCursor.getInt(0);
                }
                ignoredCursor.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "统计报警状态数量失败: " + e.getMessage());
        }
        
        return counts;
    }
    
    /**
     * 将游标数据转换为报警记录实体
     * @param cursor 数据库游标
     * @return 报警记录实体
     */
    private AlarmRecordEntity cursorToAlarmRecord(Cursor cursor) {
        AlarmRecordEntity alarmRecord = new AlarmRecordEntity();
        
        alarmRecord.setId(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_ID)));
        alarmRecord.setAlarmType(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_ALARM_TYPE)));
        alarmRecord.setParamType(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_PARAM_TYPE)));
        alarmRecord.setDeviceId(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_DEVICE_ID)));
        alarmRecord.setLocation(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_LOCATION)));
        alarmRecord.setValue(cursor.getDouble(cursor.getColumnIndex(SdgDatabase.COLUMN_VALUE)));
        alarmRecord.setUnit(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_UNIT)));
        alarmRecord.setThresholdMin(cursor.getDouble(cursor.getColumnIndex(SdgDatabase.COLUMN_THRESHOLD_MIN)));
        alarmRecord.setThresholdMax(cursor.getDouble(cursor.getColumnIndex(SdgDatabase.COLUMN_THRESHOLD_MAX)));
        alarmRecord.setLevel(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_LEVEL)));
        alarmRecord.setStatus(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_STATUS)));
        alarmRecord.setDescription(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_DESCRIPTION)));
        alarmRecord.setCreateTime(cursor.getLong(cursor.getColumnIndex(SdgDatabase.COLUMN_CREATE_TIME)));
        alarmRecord.setCreateTimeString(cursor.getString(cursor.getColumnIndex(SdgDatabase.COLUMN_CREATE_TIME_STRING)));
        
        int handleTimeIndex = cursor.getColumnIndex(SdgDatabase.COLUMN_HANDLE_TIME);
        if (handleTimeIndex != -1 && !cursor.isNull(handleTimeIndex)) {
            alarmRecord.setHandleTime(cursor.getLong(handleTimeIndex));
        }
        
        int handleUserIndex = cursor.getColumnIndex(SdgDatabase.COLUMN_HANDLE_USER);
        if (handleUserIndex != -1 && !cursor.isNull(handleUserIndex)) {
            alarmRecord.setHandleUser(cursor.getString(handleUserIndex));
        }
        
        int handleDescIndex = cursor.getColumnIndex(SdgDatabase.COLUMN_HANDLE_DESCRIPTION);
        if (handleDescIndex != -1 && !cursor.isNull(handleDescIndex)) {
            alarmRecord.setHandleDescription(cursor.getString(handleDescIndex));
        }
        
        return alarmRecord;
    }
    
    /**
     * 添加WHERE子句
     * @param whereClause WHERE子句构建器
     * @param condition 条件
     */
    private void appendWhereClause(StringBuilder whereClause, String condition) {
        if (whereClause.length() > 0) {
            whereClause.append(" AND ");
        }
        whereClause.append(condition);
    }
} 